package org.act.BPEL2Automata.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <b>Logger</b> used to log system runtime information or 
 * exceptions.It's not necessary to get the system run.
 * 
 * @author baoxiwu<bb@feijiao.info>,ACT,Beihang University
 * @version 0.9.0
 * @since 2010-03-31
 * 
 */
public class Logger {
	public static final int LOG_TYPE_EXCEPTION = 0;
	public static final int LOG_TYPE_RUNTIME_INFO = 1;

	public static final int LOG_OUTPUT_TYPE_FILE = 2;
	public static final int LOG_OUTPUT_TYPE_CONSOLE = 3;
	public static final int LOG_OUTPUT_TYPE_DO_NOTHING = 4;
	
	private int logType = Logger.LOG_TYPE_EXCEPTION;
	private int logOutputType = Logger.LOG_OUTPUT_TYPE_CONSOLE;
	
	private String defaultExceptionFilePath = "./log_exception.dat";
	private String defaultRuntimeInfoFilePath = "./log_runtime_info.dat";
	
	private String filePath = null;
	
	
	public Logger(){
	//empty
	}
	
	public Logger(int logType,int logOutputType){
		this.logType = logType;
		this.logOutputType = logOutputType;
		
		if(logType == Logger.LOG_TYPE_EXCEPTION){
			this.filePath = this.defaultExceptionFilePath;
			
		}
		else
		if(logType == Logger.LOG_TYPE_RUNTIME_INFO){
			this.filePath = this.defaultRuntimeInfoFilePath;
		}
	}
	
	public Logger(int logType,int logOutputType,String path){
		this.logType = logType;
		this.logOutputType = logOutputType;
		
		if(logType == Logger.LOG_TYPE_EXCEPTION){
			this.filePath = path;
		}
		else
		if(logType == Logger.LOG_TYPE_RUNTIME_INFO){
			this.filePath = path;
		}
	}
	
	
	public void log(String content){
		String s = null;
		
		if(this.logType == Logger.LOG_TYPE_EXCEPTION){
			s = "[" + this.getDateTime() + "] exception : " + content + "\n";
		}
		else
		if(this.logType == Logger.LOG_TYPE_RUNTIME_INFO){
			s = "[" + this.getDateTime() + "] runtime information : " + content + "\n";
		}
		
		
		if(this.logOutputType == Logger.LOG_OUTPUT_TYPE_CONSOLE){
			System.err.println(s);
		}
		else
		if(this.logOutputType == Logger.LOG_OUTPUT_TYPE_FILE){
			this.write2File(this.filePath, s);
		}
		else
		if(this.logOutputType == Logger.LOG_OUTPUT_TYPE_DO_NOTHING){
			;
		}
	}	
	
	//write <b>content</b> to files.
	private void write2File(String filePath,String content){
		try {
			FileWriter fw = new FileWriter(filePath, true);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.flush();
			
			bw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//get system date and time.
	private String getDateTime(){
		Calendar cal =Calendar.getInstance();
		Date date = cal.getTime();
		
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dateformat.format(date);
	}
}
